﻿Imports AUtility
Imports System.Net.Mail
Imports System.Data
Imports System.Data.SqlClient


Partial Public Class Login
    Inherits System.Web.UI.Page

    Dim RedirectPage As String = "http://tenisliga.blogspot.com/2010/02/mydetails.html"

    Private Sub SessionBullShit()
        'Session bullshit
        If (Application("Count") IsNot Nothing) Then
            Response.Write(Application("Count").ToString() + "<br>")
            If (Convert.ToInt16(Application("Count")) = 2) Then
                Application("Count") = Nothing
            Else
                Session("UserId") = Application("UserId")
            End If
            Application("Count") = Convert.ToInt16(Application("Count")) + 1
            If (Convert.ToInt16(Application("Count")) = 2) Then
                Application("Count") = Nothing
            End If
        End If
        'Response.Write(Session.SessionID + "<br>")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        'SessionBullShit()
        If (Session("UserId") IsNot Nothing) Then
            UserLoggedIn()
        End If

        If (Not IsPostBack) Then
            If (Request.Cookies("UserId") IsNot Nothing AndAlso Request.Cookies("UserId").Value.ToString() <> "") Then
                Session("UserId") = Request.Cookies("UserId").Value
                'Response.Redirect(RedirectPage)
                UserLoggedIn()
            End If
        End If


    End Sub

    Protected Sub btnUlaz_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUlaz.Click
        Try
            LoginUser()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Sub UserLoggedIn()
        panLogin.Visible = False
        panForgot.Visible = False
        panMyLinks.Visible = True
    End Sub

    Private Sub LoginUser()
        If Login(txtKorisnik.Text, txtLozinka.Text) Then
            UserLoggedIn()
            If chkZapamtiMe.Checked Then
                Response.Cookies("UserId").Expires = DateTime.Now.AddDays(30)
                Response.Cookies("UserId").Value = Session("UserId").ToString()
                Response.Cookies("UserId").Expires = DateTime.Now.AddDays(30)
            End If
            'ClientScript.RegisterStartupScript(Me.GetType(), "scriptid", "window.parent.location.href='http://tenisliga.blogspot.com/2010/02/mydetails.html'", True)
            'Response.Redirect(RedirectPage)
        Else
            lblPoruka.Visible = True
            lblPoruka.Text = "Prijava nije uspjela"
        End If
    End Sub

    Private Sub SetUserId(ByVal dr As SqlDataReader)
        While (dr.Read())
            Session("UserId") = dr("IgracId")
        End While
    End Sub


    Function Login(ByVal username As String, ByVal password As String) As Boolean

        Dim selectStr As String = " SELECT * FROM Igrac WHERE username = @Username AND Password = @Password "

        Dim parametri() As SqlParameter = New SqlParameter(1) {}
        parametri(0) = New SqlParameter("@Username", SqlDbType.NVarChar)
        parametri(0).Value = username
        parametri(1) = New SqlParameter("@Password", SqlDbType.NVarChar)
        parametri(1).Value = password

        Dim conn As SqlConnection = New SqlConnection(GlobalTenisLiga.ConnectionString)
        conn.Open()
        Dim cmd As SqlCommand = New SqlCommand(selectStr, conn)
        cmd.Parameters.AddRange(parametri)
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        If (reader.HasRows) Then
            SetUserId(reader)
            Return True
        Else
            reader.Close()
            selectStr = " SELECT * FROM Igrac WHERE Mail = @Username AND Password = @Password "
            cmd.CommandText = selectStr
            'parametri(0) = New SqlParameter("@Mail", SqlDbType.NVarChar)
            reader = cmd.ExecuteReader()
            If (reader.HasRows) Then
                SetUserId(reader)
                Return True
            Else
                Return False
                conn.Close()
            End If
        End If
    End Function

    Protected Sub btnSalji_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalji.Click

        Dim selectStr As String = String.Format("SELECT password FROM Igrac WHERE Mail = '{0}'", txtMailForgot.Text)
        Dim o As Object
        o = DataAccess.GetValue(GlobalTenisLiga.ConnectionString, selectStr)


        If (o IsNot Nothing) Then


            Dim mm As New MailMessage()
            Dim from As New MailAddress("teniszg@gmail.com")
            Dim mailTo As New MailAddress(txtMailForgot.Text)


            mm.From = from
            mm.To.Add(mailTo)

            mm.Subject = "Tenis liga - vaš password"
            'mm.Body = String.Format("Vaš password je: {0}\n\n http://tenisliga.blogspot.com \n{1}", o, DateTime.Now.ToString())
            mm.Body = String.Format("Vaš password je: {0}", o)

            'hosting centar
            'Dim smpt As New SmtpClient("217.14.208.72")
            Dim smpt As New SmtpClient("localhost")

            'plus
            'Dim smpt As New SmtpClient("89.201.164.205")
            'Dim SMTPUserInfo As System.Net.NetworkCredential = New System.Net.NetworkCredential("test123", "test123")
            'smpt.UseDefaultCredentials = False
            'smpt.Credentials = SMTPUserInfo
            smpt.Send(mm)

            lblMessageForgot.Visible = True
            lblMessageForgot.Text = "Poslan vam je mail sa passwordom"
        Else
            lblMessageForgot.Visible = True
            lblMessageForgot.Text = "Nije proađen taj email"
        End If


    End Sub

    Protected Sub btnZaboravio_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZaboravio.Click
        panForgot.Visible = True
    End Sub

    Protected Sub btnLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogout.Click
        panMyLinks.Visible = False
        panLogin.Visible = True

        Session("UserId") = Nothing
        Response.Cookies("UserId").Expires = DateTime.Now.AddDays(-30)
    End Sub
End Class